const app = getApp();
import req from "./request";
import { toast } from "./toast";

import { WeChartLogind } from "../messages/uris";

export function login(that) {
  wx.showLoading({
    title: "正在登陆",
    mask: true,
  });
  setTimeout(function () {
    wx.hideLoading()
  }, 5000)
  wx.login({
    success: (res) => {
      // 通过微信code登陆
      req.get(
        WeChartLogind + "?code=" + res.code,
        {},
        (resp) => {
          wx.hideLoading({});
          if (resp.data.result.userInfo) {
            wx.setStorageSync("userInfo", resp.data.result.userInfo);
          }

          console.log("微信登陆成功");
          app.globalData.login = true;
          toast("登陆成功");
        },
        (resp) => {
          wx.hideLoading({});
          console.log("登陆失败");
          toast("登陆失败", "error");

          if (resp.data && resp.data.code && resp.data.code == 410) {
            toast("未注册", "error");
            wx.redirectTo({ url: "../regist/regist" });
          } else {
            toast("登陆失败", "error");
            wx.switchTab({ url: "../index/index" });
          }
          return;
        },(res)=>{},that
      );
    },
  });
}
